package com.example.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.mapper.StudentMapper;
import com.example.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Component
@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Override
    public Student selectStudentById(Integer id) {
        return studentMapper.selectByPrimaryKey(id);
    }


    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Override
    public String randomNumb(Integer id) {
        Random random = new Random();
        String sixNumber= String.valueOf(random.nextInt(999999));
        //60s过期
        redisTemplate.opsForValue().set(String.valueOf(id),sixNumber,60, TimeUnit.SECONDS);
        return sixNumber;
    }

    @Override
    public String getNumb(String id) {
        return String.valueOf(redisTemplate.opsForValue().get(id));
    }
}
